package com.tudulist.bo.impl;

import javax.inject.Inject;
import javax.persistence.NoResultException;

import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import com.tudulist.bo.UserManager;
import com.tudulist.dao.UserDao;
import com.tudulist.model.User;

@Service
public class UserManagerImpl implements UserManager {

	@Inject
	UserDao userDao;

	public User findUser(String login) throws NoResultException {
		return userDao.find(login);
	}

	public void updateUser(User user) {
		userDao.save(user);
	}

	public User getCurrentUser() {
		SecurityContext securityContext = SecurityContextHolder.getContext();
		org.springframework.security.core.userdetails.User springSecurityUser = (org.springframework.security.core.userdetails.User) securityContext.getAuthentication()
				.getPrincipal();

		return this.findUser(springSecurityUser.getUsername());
	}

}
